﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace Fis.Win.Utils
{
    public class AppSession
    {

        public static Hashtable CreateServiceParamter(Hashtable model)
        {
            Hashtable p = model ?? new Hashtable();
            p["IsAdmin"] = IsAdminUser;
            p["SBOOKID"] = BookID;
            p["FUSERNAME"] = UserName;
            p["USERSID"] = UserSID;
            p["YEAR"] = Year;
            p["RANGE"] = Range;
            p["Version"] = System.Windows.Forms.Application.ProductVersion;
            return p;
        }

        /// <summary>
        /// 是否主管
        /// </summary>
        public static bool IsAdminUser { get; set; }
        /// <summary>
        /// 账套编号
        /// </summary>
        public static string BookID { get; set; }
        /// <summary>
        /// 用户名称
        /// </summary>
        public static string UserName { get; set; }
        /// <summary>
        /// 用户编号
        /// </summary>
        public static string UserSID { get; set; }
        /// <summary>
        /// 帐套年度
        /// </summary>
        public static string Year { get; set; }
        /// <summary>
        /// 用户密码
        /// </summary>
        public static string Password { get; set; }
        /// <summary>
        /// 当前期间
        /// </summary>
        public static string Range { get; set; }

        public static Hashtable Book { get; set; }


        public static Agile.Engine.View.IViewProvider GetViewProvider(string alias)
        {
            return Agile.Engine.ObjectContext.GetObject(alias) as Agile.Engine.View.IViewProvider;
        }

        public static void InitializeBookInfo()
        {
            Agile.Engine.Data.DataContext dc = Agile.Engine.Data.DataContext.Get("CacheDataContext");
            var book = dc.DefaultModel.QueryTable("TBOOK");
            Book = Agile.Engine.Utils.DataUtils.ToHashTable(book.Rows[0]);
            var f = System.Windows.Forms.Application.OpenForms["MainForm"] as MainForm;
            f.InitializeStatusInfo(Book);
            Range = book.Rows[0]["FBOOKRANGE"].ToString();
            Year = DateTime.Parse(book.Rows[0]["FBOOKSTARTIME"].ToString()).Year.ToString();
        }

        private static bool isNetConnect = false;
        public static bool IsNetConnect
        {
            get
            {
                //if (!isNetConnect)
                //    isNetConnect = new Fis.Win.Rules.SynchronizeViewProvider().NetStateClient();
                return isNetConnect;
            }
            set
            {
                isNetConnect = value;
                var f = System.Windows.Forms.Application.OpenForms["MainForm"] as MainForm;
                if (f != null)
                {
                    f.Invoke(
                        new Action(() =>
                    {
                        f.NetStatus = (value) ? "已连接" : "未连接";
                    }));
                }
            }
        }

        public static bool IsComplateInitialize
        {
            get { return Book == null ? false : int.Parse(Book["FBOOKINITIALIZED"].ToString()) == 1; }
        }


        public static bool RightsValidata(string type, string func)
        {

            if (IsAdminUser)
                return true;

            var data = CacheContext.GetCache("TRIGHTS");
            data.PrimaryKey = new DataColumn[]{ 
                data.Columns["FUSERID"]
                , data.Columns["FMODULE"]
                , data.Columns["FFUNC"]};

            var valid = data.Rows.Find(new object[] { UserSID, type, func.ToUpper() });

            //不在权限列表中或未授权，不能使用
            if (valid == null || !valid["FVALUE"].Equals(1)) // 未授权
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("没有足够的权限");
                return false;
            }

            return true;
        }

    }
}
